package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirAircraftMapper;
import zhongbo.java.air.dictionary.model.AirAircraft;
import zhongbo.java.air.dictionary.service.AirAircraftService;

import java.util.Optional;

/**
 * 飞机基础信息表(AirAircraft)表服务实现类
 *
 * @author makejava
 * @since 2025-03-31 13:55:18
 */
@Service("airAircraftService")
public class AirAircraftServiceImpl implements AirAircraftService<AirAircraft> {
    @Autowired
    private AirAircraftMapper airAircraftMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param entityId 主键
     * @return 实例对象
     */
//    @Override
//    public AirAircraft queryById(String id) throws Exception {
//        if (id == null){
//            throw new Exception("id不能为空");
//        }
//        Optional<AirAircraft> airAircraftOptional = Optional.ofNullable(airAircraftMapper.selectById(id));
//        if (!airAircraftOptional.isPresent()){
//            throw new Exception("id不存在");
//        }
//        return airAircraftOptional.get();
//    }
    @Override
    public AirAircraft findOne(String entityId) throws Exception {
        if (entityId == null){
            throw new Exception("id不能为空");
        }
        Optional<AirAircraft> airAircraftOptional = Optional.ofNullable(airAircraftMapper.selectById(entityId));
        if (!airAircraftOptional.isPresent()){
            throw new Exception("id不存在");
        }
        return airAircraftOptional.get();
    }



    @Override
    public PageVo<AirAircraft> findPage(int page, int pageSize, String keyword, String fields, String oderByField, boolean orderByAsc) {
        QueryWrapper<AirAircraft> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field),keyword);
            }
        }
        IPage<AirAircraft> pageParam = new Page<>(page, pageSize);
        IPage airAlertZoneIPage = airAircraftMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(airAlertZoneIPage);
    }

    /**
     * 新增数据
     *
     * @param entity 实例对象
     * @return 实例对象
     */
//    @Override
//    public AirAircraft insert(AirAircraft airAircraft) throws Exception {
//        if (airAircraft == null){
//            throw new Exception("新增失败");
//        }
//
//        // 字段重复验证
//        validateUniqueFields(airAircraft);
//
//        this.airAircraftMapper.insert(airAircraft);
//        return airAircraft;
//    }
    @Override
    public AirAircraft add(AirAircraft entity) throws Exception {
        if (entity == null){
            throw new Exception("新增失败");
        }

        // 字段重复验证
        validateUniqueFields(entity);

        this.airAircraftMapper.insert(entity);
        return entity;
    }

    /**
     * 修改数据
     *
     * @param edit 实例对象
     * @return 实例对象
     */
//    @Override
//    public AirAircraft update(AirAircraft airAircraft) throws Exception {
//        if (airAircraft == null){
//            throw new Exception("更新失败");
//        }
//
//        // 字段重复验证
//        validateUniqueFields(airAircraft);
//
//        this.airAircraftMapper.updateById(airAircraft);
//        return this.queryById(airAircraft.getId());
//    }
    @Override
    public AirAircraft edit(AirAircraft edit) throws Exception {
        if (edit == null){
            throw new Exception("更新失败");
        }

        // 字段重复验证
        validateUniqueFields(edit);

        this.airAircraftMapper.updateById(edit);
        return this.findOne(edit.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param entityId 主键
     * @return 是否成功
     */
//    @Override
//    public boolean deleteById(String id) throws Exception {
//        if (id == null){
//            throw new Exception("id不能为空");
//        }
//        return this.airAircraftMapper.deleteById(id) > 0;
//    }
    @Override
    public boolean delete(String entityId) throws Exception {
        if (entityId == null){
            throw new Exception("id不能为空");
        }
        return this.airAircraftMapper.deleteById(entityId) > 0;
    }


    // 添加字段重复验证方法
    private void validateUniqueFields(AirAircraft airAircraft) throws Exception {
        // 验证 icaoCode 和 registration 的唯一性
        if (airAircraftMapper.selectByIcaoCode(airAircraft.getIcaoCode()) != null) {
            throw new Exception("ICAO 24位地址码已存在");
        }
        if (airAircraftMapper.selectByRegistration(airAircraft.getRegistration()) != null) {
            throw new Exception("飞机注册号已存在");
        }
    }
}
